﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;

namespace MySCM.Agent.Xml
{
    public class ConfigXmlAgent
    {
        public XmlDocument configDoc;

        
        public ConfigXmlAgent(string ConfigFile)
        {
            string configFilename = ConfigFile;
            XmlDocument configDoc = new XmlDocument();
            try
            {
                configDoc.Load(configFilename);
                if (configDoc != null)
                    this.configDoc = configDoc;
                else
                {
                    // Error Doc
                    XmlDocument errDoc = new XmlDocument();
                    XmlNode docNode = errDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                    errDoc.AppendChild(docNode);

                    XmlNode errRoot = errDoc.CreateElement("Errors");
                    errDoc.AppendChild(errRoot);

                    // === For Developers
                    XmlNode errNode = errDoc.CreateElement("Error");
                    XmlAttribute id = errDoc.CreateAttribute("id");
                    errNode.Attributes.Append(id);
                    errNode.Attributes["id"].Value = "Dev";
                    errNode.Value = "configDoc is NULL";
                    errRoot.AppendChild(errNode);

                    // === For Users
                    errNode = errDoc.CreateElement("Error");
                    errNode.Attributes.Append(id);
                    errNode.Attributes["id"].Value = "Usr";
                    errNode.Value = "Can not LOAD config file - " + ConfigFile;
                    errRoot.AppendChild(errNode);
                    this.configDoc = errDoc;
                }
            }
            catch (Exception e)
            {
                // Error Doc
                XmlDocument errDoc = new XmlDocument();
                XmlNode docNode = errDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                errDoc.AppendChild(docNode);

                XmlNode errRoot = errDoc.CreateElement("Errors");
                errDoc.AppendChild(errRoot);

                XmlNode errNode = errDoc.CreateElement("Error");
                XmlAttribute id = errDoc.CreateAttribute("id");
                errNode.Attributes.Append(id);
                errNode.Attributes["id"].Value = "Developer";
                errNode.InnerText = "Load Exception: " + e.Message;
                errRoot.AppendChild(errNode);

                // === For Users
                errNode = errDoc.CreateElement("Error");
                errNode.Attributes.Append(id);
                errNode.Attributes["id"].Value = "Usr";
                errNode.InnerText = "Can not LOAD config file - " + ConfigFile;
                errRoot.AppendChild(errNode);
                this.configDoc = errDoc;
            }
        }


        public string GetNodeValue(string nodeName)
        {
            string returnNodeValue = "";
            try
            {
                returnNodeValue = this.configDoc.SelectSingleNode("//" + nodeName).InnerText;
            }
            catch (Exception e)
            {
                returnNodeValue = "Node (" + nodeName + ") Not Found - " + e.Message;
            }
            return returnNodeValue;
        }
    }
}
